package com.cqrt.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cqrt.dto.WorkItemDTO;
import com.cqrt.entity.ChangeIssueDO;
import com.cqrt.service.RtWorkItemService;
import com.cqrt.util.PageDtoUtil;
import com.cqrt.vo.WorkItemVO;
import com.github.pagehelper.PageInfo;
import com.hustcad.plm.pdm.container.model.dto.search.ProjectEntityParamDTO;
import com.hustcad.plm.pdm.container.model.vo.search.ProjectEntityVO;
import com.hustcad.plm.pdm.core.response.TyppmResponseResult;
import com.hustcad.plm.rpm.model.dto.job.ProjectTaskDTO;
import com.hustcad.plm.rpm.model.dto.job.TyppmJobDTO;
import com.hustcad.plm.rpm.project.service.TyppmProjectService;
import com.ty.basic.response.ResponseResult;
import com.ty.ppm.model.entity.job.Job;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/v1/workItem")
public class RTWorkItemController {

    @Resource
    private RtWorkItemService rtWorkItemService;

    @Resource
    private TyppmProjectService typpmProjectService;

    /**
     * 查询我管理的工作任务
     * @return
     */
    @GetMapping("/queryWorkItem")
    public ResponseResult queryWorkItem(
                                        @RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "20")Integer pageSize) {
        List<WorkItemVO> workItemVOS = rtWorkItemService.queryWorkItem(null);
        return ResponseResult.success(PageDtoUtil.getPageDto(workItemVOS,pageNum,pageSize));
    }

    /**
     * 上报工作任务
     * @param workItemDTO
     * @return
     */
    @PostMapping("/insertUserWorkItem")
    public ResponseResult insertUserWorkItemLink(@RequestBody WorkItemDTO workItemDTO){
        try {
            rtWorkItemService.insertUserWorkItemLink(workItemDTO);
        }catch (Exception e){
            return ResponseResult.fail(ResponseResult.ERROR,e.getMessage());
        }
        return ResponseResult.success();
    }
    /**
     * 查询我管理的项目任务
     */
    @GetMapping("/queryProjectTask")
    public ResponseResult queryProjectTask(@RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "20")Integer pageSize){
        List<Job> jobs = rtWorkItemService.queryProjectTask();

        return ResponseResult.success(PageDtoUtil.getPageDto(jobs,pageNum,pageSize));
    }

    /**
     * 查询我管理的问题
     */
    @GetMapping("/queryIManageQuestions")
    public ResponseResult queryIManageQuestions(@RequestParam(defaultValue = "1") Integer pageNum,
                                                @RequestParam(defaultValue = "20")Integer pageSize){
        List<ChangeIssueDO> changeIssueDOS = rtWorkItemService.queryIManageQuestions();
        return ResponseResult.success(PageDtoUtil.getPageDto(changeIssueDOS,pageNum,pageSize));
    }

    @PostMapping({"/queryProjectAdvancedSearch"})
    @ApiOperation(
            value = "获取项目库列表",
            produces = "application/json; charset=utf-8"
    )
    public TyppmResponseResult<JSONObject> queryProjectAdvancedSearch(@RequestBody ProjectEntityParamDTO params) {
        params.setPageSize(99999);
        PageInfo<ProjectEntityVO> pageInfo = this.typpmProjectService.queryProjectAdvancedSearch(params);
        List<ProjectEntityVO> l = pageInfo.getList();
        List<BigInteger> oids = l.stream().map(ProjectEntityVO::getOid).collect(Collectors.toList());
        List<BigInteger> ls=rtWorkItemService.selectOidByPjStatus(oids);

        pageInfo.setList(l.stream().filter(p -> ls.contains(p.getOid())).collect(Collectors.toList()));
        pageInfo.setPageSize(ls.size());
        JSONObject json = (JSONObject) JSON.toJSON(pageInfo);
        JSONArray list = (JSONArray)json.get("list");

        for(int i = 0; Objects.nonNull(list) && i < list.size(); ++i) {
            JSONObject obj = (JSONObject)list.get(i);
            obj.put("oid", obj.get("oid").toString());
        }

        return TyppmResponseResult.success(json);
    }

}
